AI Resource Pro / website · airesourcepro.com / src / app / api / account / [section] / route.ts
| 1 | import { accountStore } from "@/lib/account-store"; |
| 2 | import { parseSurvey } from "@/lib/ai-survey"; |
| 3 | import { parseProfile } from "@/lib/onboarding"; |
| 4 | import { sessionUser } from "@/lib/session-server"; |
| 5 | |
| 6 | function response(data: unknown, status = 200) { |
| 7 | return Response.json(data, { status, headers: { "Cache-Control": "private, no-store" } }); |
| 8 | } |
| 9 | |
| 10 | function tutorialKey(value: unknown): value is string { |
| 11 | return typeof value === "string" && value.length <= 200 && /^[a-z0-9-]+\/[a-z0-9-]+$/.test(value); |
| 12 | } |
| 13 | |
| 14 | type RouteContext = { params: Promise<{ section: string }> }; |
| 15 | |
| 16 | export async function GET(request: Request, { params }: RouteContext) { |
| 17 | const { section } = await params; |
| 18 | try { |
| 19 | const user = await sessionUser(request); |
| 20 | if (!user) return response({ error: "Sign in to continue." }, 401); |
| 21 | const store = accountStore(user); |
| 22 | switch (section) { |
| 23 | case "saved": return response({ data: await store.saved() }); |
| 24 | case "profile": return response({ data: await store.profile() }); |
| 25 | case "progress": return response({ data: await store.progress() }); |
| 26 | case "survey": return response({ data: await store.survey() }); |
| 27 | default: return response({ error: "Account section not found." }, 404); |
| 28 | } |
| 29 | } catch { |
| 30 | console.error("[account] Read failed"); |
| 31 | return response({ error: "Could not load your account." }, 500); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | export async function POST(request: Request, { params }: RouteContext) { |
| 36 | if (request.headers.get("origin") !== new URL(request.url).origin) { |
| 37 | return response({ error: "Request origin was rejected." }, 403); |
| 38 | } |
| 39 | |
| 40 | const { section } = await params; |
| 41 | try { |
| 42 | const user = await sessionUser(request); |
| 43 | if (!user) return response({ error: "Sign in to continue." }, 401); |
| 44 | const body = await request.json().catch(() => null); |
| 45 | if (!body || typeof body !== "object") return response({ error: "Invalid request." }, 400); |
| 46 | |
| 47 | const store = accountStore(user); |
| 48 | switch (section) { |
| 49 | case "saved": |
| 50 | if (!tutorialKey(body.key) || typeof body.saved !== "boolean") return response({ error: "Invalid tutorial." }, 400); |
| 51 | await store.setSaved(body.key, body.saved); |
| 52 | break; |
| 53 | case "profile": { |
| 54 | const profile = parseProfile(body.profile); |
| 55 | if (!profile) return response({ error: "Invalid learning profile." }, 400); |
| 56 | return response({ data: await store.setProfile(profile) }); |
| 57 | } |
| 58 | case "progress": { |
| 59 | const keys: unknown[] = Array.isArray(body.keys) ? body.keys : []; |
| 60 | if (!keys.length || keys.length > 100 || !keys.every(tutorialKey) || typeof body.completed !== "boolean") { |
| 61 | return response({ error: "Invalid tutorial progress." }, 400); |
| 62 | } |
| 63 | await store.setProgress([...new Set(keys)], body.completed); |
| 64 | break; |
| 65 | } |
| 66 | case "survey": |
| 67 | // null clears the answers; a survey with nothing answered clears them too |
| 68 | if (body.survey !== null && (typeof body.survey !== "object" || Array.isArray(body.survey))) { |
| 69 | return response({ error: "Invalid answers." }, 400); |
| 70 | } |
| 71 | return response({ data: await store.setSurvey(parseSurvey(body.survey)) }); |
| 72 | case "view": |
| 73 | if (!tutorialKey(body.key)) return response({ error: "Invalid tutorial." }, 400); |
| 74 | await store.recordView(body.key); |
| 75 | break; |
| 76 | default: |
| 77 | return response({ error: "Account section not found." }, 404); |
| 78 | } |
| 79 | |
| 80 | return response({ data: null }); |
| 81 | } catch { |
| 82 | console.error("[account] Write failed"); |
| 83 | return response({ error: "Could not save your account changes." }, 500); |
| 84 | } |
| 85 | } |